const config = {
    keyPrefix: 'frameWork'
}

const dataStore = (() => {
    const store = {};
    const listeners = {};

    const notifyListeners = (key) => {
        if (listeners[key]) {
            listeners[key].forEach(callback => callback(store[key]));
        }
    };

    const changeKey = (key) => {
        return config.keyPrefix + '_' + key
    }

    return {
        setItem(key, value) {
            key = changeKey(key)
            store[key] = value;
            notifyListeners(key);
        },

        getItem(key) {
            key = changeKey(key)
            return store[key];
        },

        removeItem(key) {
            key = changeKey(key)
            delete store[key];
            notifyListeners(key);
        },

        onChange(key, callback) {
            key = changeKey(key)
            if (!listeners[key]) {
                listeners[key] = [];
            }
            listeners[key].push(callback);
        }
    };
})();

export default dataStore;